﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BCCommon;
using System.Text;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using BCDto.Sim.Company.Position;
using BCTMSRESTful.Configs;
using BCService.Utils;
using BCService.Sim.Company.Position;

namespace BCTMSRESTful.Controllers.Management.Organization.Position
{
    /// <summary>
    /// 岗位
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class PositionController : ManagementBaseController
    {
        private readonly IPositionService positionService;

        public PositionController(IPositionService positionService)
        {
            this.positionService = positionService;
        }

        #region 添加岗位
        /// <summary>
        ///  添加岗位
        /// </summary>
        /// <param name="requestDto">岗位信息</param>
        /// <response code="201">添加岗位成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加岗位")]
        [ProducesResponseType(typeof(PositionDto), 201)]
        public IActionResult Post(
            [FromBody] PositionRequestDto requestDto)
        {
            var result = this.positionService.Add(requestDto,this.Admin);
            return StatusCode((int)HttpStatusCode.Created, result);
        }
        #endregion

        #region 修改岗位
        /// <summary>
        ///  修改岗位
        /// </summary>
        /// <param name="positionId">岗位Id</param>
        /// <param name="requestDto">岗位信息</param>
        /// <response code="200">修改岗位成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("{position_id}")]
        [Log("修改岗位")]
        [ProducesResponseType(typeof(PositionDto), 200)]
        public IActionResult Put([FromRoute(Name = "position_id")] int positionId, [FromBody] PositionRequestDto requestDto)
        {
            PositionPutDto putDto = requestDto.As<PositionPutDto>();
            putDto.PositionId = positionId;
            return StatusCode((int)HttpStatusCode.OK, this.positionService.Update(putDto,this.Admin));
        }
        #endregion

        #region 删除岗位
        /// <summary>
        ///  删除岗位
        /// </summary>
        /// <param name="positionId">岗位Id</param>
        /// <response code="204">删除岗位成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpDelete("{position_id}")]
        [Log("删除岗位")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Delete([FromRoute(Name = "position_id")] int positionId)
        {
            this.positionService.Delete(positionId);
            return StatusCode((int)HttpStatusCode.NoContent);
        }
        #endregion

        #region 获取岗位
        /// <summary>
        ///  获取岗位
        /// </summary>
        /// <param name="positionId">岗位Id</param>
        /// <response code="200">获取岗位</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("{position_id}")]
        [ProducesResponseType(typeof(PositionDto), 200)]
        public IActionResult Get([FromRoute(Name = "position_id")] int positionId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.positionService.Get(positionId));
        }
        #endregion

        #region 获取岗位列表
        /// <summary>
        ///  获取岗位列表
        /// </summary>
		/// <param name="companyId">公司Id</param>
		/// <param name="departmentId">部门Id</param>
        /// <response code="200">获取岗位列表</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]/{company_id}")]
        [ProducesResponseType(typeof(IEnumerable<PositionDto>), 200)]
        public IActionResult List(
            [FromRoute(Name = "company_id")] int companyId,
            [FromQuery(Name = "department_id")] int? departmentId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.positionService.GetList(companyId,departmentId,true));
        }
        #endregion

        #region 获取岗位树形列表
        /// <summary>
        ///  获取岗位树形列表
        /// </summary>
        /// <param name="companyId">公司Id</param>
		/// <param name="departmentId">部门Id</param>
        /// <response code="200">获取岗位树形列表</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]/{company_id}")]
        [ProducesResponseType(typeof(List<object>), 200)]
        public IActionResult PositionTree([FromRoute(Name = "company_id")] int companyId,
            [FromQuery(Name = "department_id")] int? departmentId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.positionService.GetPositionTree(companyId, departmentId));
        }
        #endregion

        #region 获取岗位（分页）
        /// <summary>
        ///  获取岗位（分页）
        /// </summary>
		/// <param name="companyId">公司Id</param>
		/// <param name="departmentId">部门Id</param>
        /// <param name="enabled">是否启用</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">升序</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">当前页大小</param>
        /// <response code="200">获取岗位（分页）</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<PositionDto>), 200)]
        public IActionResult Query(
            [FromQuery(Name = "company_id")] int? companyId,
            [FromQuery(Name = "department_id")] int? departmentId,
            [FromQuery(Name = "enabled")] bool? enabled,
            [FromQuery(Name = "start")] DateTime? start,
            [FromQuery(Name = "end")] DateTime? end,
            [FromQuery(Name = "sort_name")] string sortName,
            [FromQuery(Name = "ascending")] bool? ascending,
            [FromQuery(Name = "page_index")] int pageIndex = 1,
            [FromQuery(Name = "page_size")] int pageSize = 10)
        {
            return StatusCode((int)HttpStatusCode.OK, this.positionService.Query(companyId, departmentId, enabled, start, end, sortName, ascending, pageIndex, pageSize));
        }
        #endregion

        #region 启用/禁用岗位
        /// <summary>
        ///  启用/禁用岗位
        /// </summary>
        /// <param name="positionId">岗位Id</param>
        /// <param name="enabled">是否启用</param>
        /// <response code="204">启用/禁用岗位成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("[action]/{position_id}/{enabled}")]
        [Log("启用/禁用岗位")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Enabled([FromRoute(Name = "position_id")] int positionId,
            [FromRoute(Name = "enabled")] bool enabled)
        {
            if (enabled)
            {
                this.positionService.Enabled(positionId);
                return StatusCode((int)HttpStatusCode.NoContent);
            }
            else
            {
                this.positionService.Disabled(positionId);
                return StatusCode((int)HttpStatusCode.NoContent);
            }
        }
        #endregion
    }
}

